using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Survivor;
using UnityEngine;

namespace Gameplay.PVE.Survivor
{
    public class IsTargetInRange : SurvivorHavior
    {
        public override ETaskResult Execute(ETaskResult result,bool last)
        {
            //临时处理，如果是敌方，则一旦进入攻击就一直攻击直到结束
            if (unit.team == 2 && unit.IsShooting())
            {
                return ETaskResult.Successed;
            }
            var radius = unit.target.collisionRadius;
            var diffToSelf = unit.target.Position - unit.Position;
            var attackRange = unit.GetAttackRange() + radius;
            bool isInRange = false;
            if (unit.team == 0)
            {
                var diffToCenter = unit.target.Position - unit.BattlePosition;

                var diffSqr = Mathf.Min(diffToCenter.sqrMagnitude, diffToSelf.sqrMagnitude);
                isInRange = diffSqr <= attackRange * attackRange;
            }
            else
            {
                var diffSqr = diffToSelf.sqrMagnitude;
                isInRange = diffSqr <= attackRange * attackRange;
                if (isInRange)
                {
                    if (attackRange < 3)
                    {
                        isInRange = true;
                    }
                    var attackRangeNear = attackRange - 1;
                    var isInRangeNear = diffSqr <= attackRangeNear * attackRangeNear;
                    if (isInRangeNear)
                    {
                        isInRange = true;
                    }
                    else
                    {
                        isInRange = false;
                    }
                }
                else
                {
                    isInRange = false;
                }
            }
            if (isInRange)
            {
                if (unit.IsTargetBlockByWall())
                {
                    return ETaskResult.Failed;
                }
                else
                {
                    return ETaskResult.Successed;
                }
            }
            else
            {
                return ETaskResult.Failed;
            }
            /*if (unit.IsTargetInRange())
            {
                return ETaskResult.Successed;
            }
            else
            {
                return ETaskResult.Failed;
            }*/
        }
    }
}